From a9f650b02bc28bc6bdcab8441c452f70ed26a651 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 Oct 2014 18:57:33 -0700 Subject: [PATCH] Don't always run doc tests for the root package When using `cargo test -p`, be sure to run only the doc tests for the package actually being tested. Closes #660 --- src/cargo/ops/cargo_clean.rs | 2 +- src/cargo/ops/cargo_rustc/compilation.rs | 6 ++- src/cargo/ops/cargo_rustc/context.rs | 5 ++- src/cargo/ops/cargo_rustc/mod.rs | 4 +- src/cargo/ops/cargo_test.rs | 2 +- tests/test_cargo_test.rs | 47 ++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index cb8786a47..881ebf342 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -49,7 +49,7 @@ pub fn clean(manifest_path: &Path, opts: &mut CleanOptions) -> CargoResult<()> { let pkgs = PackageSet::new([]); let cx = try!(Context::new("compile", &resolve, &srcs, &pkgs, &mut cfg, Layout::at(root.get_absolute_target_dir()), - None)); + None, &pkg)); // And finally, clean everything out! for target in pkg.get_targets().iter() { diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 2e78d0b46..8e8ddf695 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -35,10 +35,13 @@ pub struct Compilation { /// Extra environment variables that were passed to compilations and should /// be passed to future invocations of programs. pub extra_env: HashMap>, + + /// Top-level package that was compiled + pub package: Package, } impl Compilation { - pub fn new() -> Compilation { + pub fn new(pkg: &Package) -> Compilation { Compilation { libraries: HashMap::new(), native_dirs: HashMap::new(), @@ -47,6 +50,7 @@ impl Compilation { tests: Vec::new(), binaries: Vec::new(), extra_env: HashMap::new(), + package: pkg.clone(), } } diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 8f5d1d3ca..e3762012f 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -38,7 +38,8 @@ pub struct Context<'a, 'b> { impl<'a, 'b> Context<'a, 'b> { pub fn new(env: &'a str, resolve: &'a Resolve, sources: &'a SourceMap, deps: &'a PackageSet, config: &'b mut Config<'b>, - host: Layout, target: Option) + host: Layout, target: Option, + root_pkg: &Package) -> CargoResult> { let (target_dylib, target_exe) = try!(Context::filename_parts(config.target())); @@ -66,7 +67,7 @@ impl<'a, 'b> Context<'a, 'b> { target_exe: target_exe, host_dylib: host_dylib, requirements: HashMap::new(), - compilation: Compilation::new(), + compilation: Compilation::new(root_pkg), }) } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index f1428c57e..1731c44e8 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -54,7 +54,7 @@ pub fn compile_targets<'a>(env: &str, targets: &[&'a Target], pkg: &'a Package, config: &'a mut Config<'a>) -> CargoResult { if targets.is_empty() { - return Ok(Compilation::new()) + return Ok(Compilation::new(pkg)) } debug!("compile_targets; targets={}; pkg={}; deps={}", targets, pkg, deps); @@ -67,7 +67,7 @@ pub fn compile_targets<'a>(env: &str, targets: &[&'a Target], pkg: &'a Package, }); let mut cx = try!(Context::new(env, resolve, sources, deps, config, - host_layout, target_layout)); + host_layout, target_layout, pkg)); let mut queue = JobQueue::new(cx.resolve, deps, cx.config); // First ensure that the destination directory exists diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index cb8121668..3a2b61fc3 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -42,7 +42,7 @@ pub fn run_tests(manifest_path: &Path, if options.compile_opts.env == "bench" { return Ok(None) } - let mut libs = package.get_targets().iter().filter_map(|target| { + let mut libs = compile.package.get_targets().iter().filter_map(|target| { if !target.get_profile().is_doctest() || !target.is_lib() { return None } diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 07a7656db..19bb943c8 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -1053,3 +1053,50 @@ test!(build_then_selective_test { .arg("-p").arg("b"), execs().with_status(0)); }) + +test!(selective_testing_with_docs { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies.d1] + path = "d1" + "#) + .file("src/lib.rs", r#" + /// ``` + /// not valid rust + /// ``` + pub fn foo() {} + "#) + .file("d1/Cargo.toml", r#" + [package] + name = "d1" + version = "0.0.1" + authors = [] + "#) + .file("d1/src/lib.rs", ""); + p.build(); + + assert_that(p.process(cargo_dir().join("cargo")).arg("test") + .arg("-p").arg("d1"), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} d1 v0.0.1 ({dir}) +{running} target[..]deps[..]d1[..] + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured + +{doctest} d1 + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured + +", compiling = COMPILING, running = RUNNING, dir = p.url(), + doctest = DOCTEST).as_slice())); +}) -- 2.30.2